package com.guigu.qingcheng.service;

import com.alibaba.fastjson.JSON;
import com.guigu.qingcheng.util.SmsUtil;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.Map;


@Component
public class SmsMessageConsumer {

    @Autowired
    private SmsUtil util;

    @Value("${smsCode}")
    private String smsCode;

    @Value("${param}")
    private String param;

    @RabbitListener(queues = "queue.sms")
    public void onMessage(Message message) {
        String jsonString = new String(message.getBody());
        Map<String, String> map = JSON.parseObject(jsonString, Map.class);
        String phone = map.get("phone");
        String code = map.get("code");
        //控制台打印
//        System.out.println("手机号：" + phone + ",验证码：" + code);
        // 发送到用户手机上
        // param: {"code":"[value]"}
        String templateParam = param.replace("[value]",code);
        util.sendSms(phone, smsCode, templateParam);
    }
}
